<?php
//error_reporting(E_ALL);

include_once 'common_classes.php';

$loggedin = false;

session_start();
if(IsSet($_SESSION['candidate']) && IsSet($_SESSION['election']) && IsSet($_SESSION['candidate_id']))
	$loggedin=true;

function printRadioButtonDetails($it, $answer) {
	$return = '';
	switch($it) {
		case 0;
			$return = 'value="A"';
		break;
		case 1;
			$return = 'value="B"';
		break;
		case 2;
			$return = 'value="C"';
		break;
		case 3;
			$return = 'value="D"';
		break;
		case 4;
			$return = 'value="E"';
		break;
	}
	
	if($it == $answer) {
		$return = $return . ' checked="true"';
	}
	
	return $return;
}	
	
function printQuestions($location, $extra) {
	global $loggedin;
	
	if(!$extra)
		$loggedin = false;
	
	$questions = QueryBuilder::getAllQuestionsForCurrentElection();
	
	$answers;
	if($loggedin)
		$answers = QueryBuilder::getAllAnswersForCandidateID($_SESSION['candidate_id']);
?>

<form action="<?php echo $location; ?>" method="post" name="questions">
<table border="1">
	<tr class="header">
		<td class="header"><b>Issue</b></td>
		<td class="header"><b>Strongly disagree</b></td>
		<td class="header"><b>Disagree</b></td>
		<td class="header"><b>No opinion<br />
		Don't know</b></td>
		<td class="header"><b>Agree</b></td>
		<td class="header"><b>Strongly agree</b></td>
<?php
if($loggedin)
	echo '<td class="header"><b>Important question</b></td>'; 
?>
	</tr>
<?php
	$i = 0;
	// loop through the array
	while(IsSet($questions[0][$i])) {
		// write form table entries
		echo '<tr><td class="question">' . $questions[0][$i];
		
		if($loggedin) { // display explanation form / link
			echo '<div class="explain"><a onclick="showHideDiv(\'ef' . $i . '\')">[Explain answer]</a></div>';
			echo '<div class="explainform" id="ef' . $i . '"  style="display: none;"><input type="text" name="ef_' . $questions[1][$i] . '" size="92" value="' . stripslashes($answers[$questions[1][$i]][3]) . '" /></div>';
		}
		
		echo '</td>';
		
		$ans = -3;
		if($loggedin)
			$ans = $answers[$questions[1][$i]][0];
			
		for($k = 0; $k < 5; $k++) {
			echo '<td><input type="radio" name="' . $questions[1][$i] . '" '. printRadioButtonDetails($k, $ans+2) . '></input></td>';
		}
		// if we are a candidate, add a checkbox to indicate important questions
		if($loggedin) {
			$string = '';
			if($answers[$questions[1][$i]][1] == 1)
				$string = 'checked="true "';
				
			echo '<td><input type="checkbox" name="important_' . $questions[1][$i] . '" ' . $string . '/></td>';
		} 

		// close row
		echo '</tr>';
		$i++;
		
		// after every 10th row, repeat the column headers
		if($i % 10 == 0) {
			echo '<tr class="header"><td class="header"><b>Issue</b></td><td class="header"><b>Strongly disagree</b></td><td class="header"><b>Disagree</b></td><td class="header"><b>No opinion<br />Don\'t know</b></td><td class="header"><b>Agree</b></td><td class="header"><b>Strongly agree</b></td>';
			
			if($loggedin)
				echo '<td class="header"><b>Important question</b></td>'; 
				
			echo '</tr>';
		}
	}
?>
</table> <br />
<input type="submit" onClick="javascript: return validateForm();" /></form>
<?php 
}?>
